package com.onec.service.route.service.base.impl;

import com.onec.service.api.dto.request.route.RouteReq;
import com.onec.service.api.enums.SystemTypeEnum;
import com.onec.service.api.enums.error.MerchantErrorEnum;
import com.onec.service.api.exception.RouteException;
import com.onec.service.route.entity.TradeRuleInfo;
import com.onec.service.route.service.base.BaseTradeConfigDefaultService;
import org.springframework.stereotype.Service;

/**
 * @author onec
 * @Cacheable 一般应用在service中：
 * 代理商业务逻辑处理：
 */
@Service("AGENT_ID_CARD_LIMIT_SERVICE")
public class AgentLimitInfoIdCardServiceImpl extends BaseTradeConfigDefaultService {


    /**
     * 验证是否超过机构配置的限制：
     *
     * @param onlineParam   bizContent
     * @param tradeRuleInfo
     * @return false 通过：true 未通过：
     */
    @Override
    public boolean beforeExecute(RouteReq onlineParam, TradeRuleInfo tradeRuleInfo) {

        boolean isOk = agentLimitInfoService.isOk(onlineParam.getAgentNo(),
                SystemTypeEnum.ID_CARD, onlineParam.getIdCard(), Integer.valueOf(tradeRuleInfo.getValue()));
        if (!isOk) {
            throw new RouteException(MerchantErrorEnum.REGISTER_LIMIT.getIndex(),
                    MerchantErrorEnum.REGISTER_LIMIT.getMsg());
        }
        return false;
    }

    /**
     * 异步执行，新增限制数据：
     *
     * @param routeReq
     * @return
     */
    @Override
    public boolean afterExecute(RouteReq routeReq) {
        agentLimitInfoService.addAgentInfoLimit(routeReq.getAgentNo(), SystemTypeEnum.ID_CARD, routeReq.getIdCard());
        return false;
    }
}
